package com.xique.door.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xique.common.core.text.Convert;
import com.xique.common.core.web.domain.AjaxResult;
import com.xique.door.bean.domain.Area;
import com.xique.door.bean.domain.ParkInfo;
import com.xique.door.mapper.AreaMapper;
import com.xique.door.project.miniprogram.bean.request.MiniAreaCodeListRequest;
import com.xique.door.project.miniprogram.bean.vo.MIniAreaVO;
import com.xique.door.service.IAreaService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 地区码Service业务层处理
 *
 * @author ww
 * @date 2019-10-06
 */
@Service
public class AreaServiceImpl extends ServiceImpl<AreaMapper, Area> implements IAreaService {
  @Autowired private AreaMapper tAreaMapper;

  @Autowired private IAreaService areaService;
  /**
   * 查询地区码
   *
   * @param areaId 地区码ID
   * @return 地区码
   */
  @Override
  public Area selectAreaById(Long areaId) {
    return tAreaMapper.selectAreaById(areaId);
  }

  @Override
  public List<Area> selectAreaByIds(List<Long> areaIds) {
    if (CollectionUtils.isEmpty(areaIds)) {
      return new ArrayList<>(0);
    }
    return tAreaMapper.selectAreaByIds(areaIds);
  }

  /**
   * 查询地区码
   *
   * @param areaCode 地区码编号
   * @return 地区码
   */
  @Override
  public Area selectAreaByCode(String areaCode) {
    return tAreaMapper.selectAreaByCode(areaCode);
  }
  /**
   * 查询地区码列表
   *
   * @param tArea 地区码
   * @return 地区码
   */
  @Override
  public List<Area> selectAreaList(Area tArea) {
    return tAreaMapper.selectAreaList(tArea);
  }

  /**
   * 新增地区码
   *
   * @param tArea 地区码
   * @return 结果
   */
  @Override
  public int insertArea(Area tArea) {
    return tAreaMapper.insertArea(tArea);
  }

  /**
   * 修改地区码
   *
   * @param tArea 地区码
   * @return 结果
   */
  @Override
  public int updateArea(Area tArea) {
    return tAreaMapper.updateArea(tArea);
  }

  /**
   * 删除地区码对象
   *
   * @param ids 需要删除的数据ID
   * @return 结果
   */
  @Override
  public int deleteAreaByIds(String ids) {
    return tAreaMapper.deleteAreaByIds(Convert.toStrArray(ids));
  }

  /**
   * 删除地区码信息
   *
   * @param areaId 地区码ID
   * @return 结果
   */
  @Override
  public int deleteAreaById(Long areaId) {
    return tAreaMapper.deleteAreaById(areaId);
  }

  @Override
  public Map<Long, String> selectAreaName(List<Long> ids) {
    if (CollectionUtils.isEmpty(ids)) {
      return new HashMap<>(0);
    }

    List<Area> list = this.selectAreaByIds(ids);
    if (CollectionUtils.isEmpty(list)) {
      return new HashMap<>(0);
    }

    return list.stream().collect(Collectors.toMap(Area::getAreaId, Area::getAreaName));
  }

  @Override
  public List<Area> selectAreaByNames(List<String> names) {
    return tAreaMapper.selectAreaByNames(names);
  }

  public AjaxResult getAreaCodeList(MiniAreaCodeListRequest request) {

    Long parentId = -1L;
    if (!"000000".equals(request.getCode())) {
      QueryWrapper<Area> condition = new QueryWrapper<>();
      condition.eq("area_code", request.getCode());
      Area area = areaService.getOne(condition);

      parentId = area.getAreaId();
    }

    QueryWrapper<Area> wrapper = new QueryWrapper<>();
    wrapper.eq("parent_id", parentId);
    List<Area> list = areaService.list(wrapper);
    List<MIniAreaVO> volist = new ArrayList<>();
    list.stream()
        .forEach(
            item -> {
              MIniAreaVO vo = new MIniAreaVO();
              vo.setAreaCode(item.getAreaCode());
              vo.setAreaName(item.getAreaName());
              volist.add(vo);
            });

    return AjaxResult.success(volist);
  }
  /** 根据定位的经纬度获取小区list */
  public AjaxResult selectParkInfoList(String longitude, String latitude) {
    List<ParkInfo> list = tAreaMapper.selectParkInfoList(longitude, latitude);
    if (list.size() > 0) {
      return AjaxResult.success(list.get(0));
    }
    return AjaxResult.error("请维护经纬度");
  }
  ;
}
